/* 
 * Licensed Materials - Property of IBM
 * (c) Copyright IBM Corporation 2010. All Rights Reserved.
 *
 * Note to U.S. Government Users Restricted Rights:  Use,
 * duplication or disclosure restricted by GSA ADP Schedule
 * Contract with IBM Corp.
 *
 * This code is released under the terms of the Eclipse Public License
 * version 1.0 which can be found at the top level of this project or
 * or at http://www.eclipse.org/org/documents/epl-v10.php
 */

package com.ibm.mining;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import com.ibm.team.repository.client.IItemManager;
import com.ibm.team.repository.client.ITeamRepository;
import com.ibm.team.repository.common.IItem;
import com.ibm.team.repository.common.IItemHandle;
import com.ibm.team.repository.common.TeamRepositoryException;
import com.ibm.team.scm.common.IChange;
import com.ibm.team.workitem.common.model.Identifier;

public class Util {

	public static java.sql.Timestamp dateToTimestamp(java.util.Date date) {
		if (date == null) {
			Logger.getLogger(Logger.GLOBAL_LOGGER_NAME).log(Level.ALL, "Got a date that is null...");
			return null;
		}
		java.sql.Timestamp ts = new java.sql.Timestamp(date.getTime());
		if (ts.getYear() > 2010) {
			Logger.getLogger("com.ibm.mining").severe("timestamp is not a valid date: " + ts);
			Calendar cal = Calendar.getInstance();
			cal.set(2020, 1, 1, 0, 0, 0);
			return new Timestamp(cal.getTimeInMillis());
		}
		return ts;
	}
	
	public static String kindString(int kind) {
		switch (kind) {
    	case IChange.ADD: return "ADD";
    	case IChange.DELETE: return "DELETE";
    	case IChange.MODIFY: return "MODIFY";
    	case IChange.RENAME: return "RENAME";
    	case IChange.REPARENT: return "REPARENT";
    	case IChange.NONE: return "NONE";
    	default: return "UNKNOWN";
    	}
	}
	
	public static String getIdentifierString(Identifier identifier) {
		if (identifier == null) {
			Logger.getLogger(Logger.GLOBAL_LOGGER_NAME).log(Level.ALL, 
					"Got an identifier that is null...");
			return null;
		} else {
			//return identifier.getStringIdentifier();
			return identifier.getStringIdentifier().toString();
		}
	}
	
	public static String join(Collection s, String delimiter) {
        StringBuffer buffer = new StringBuffer();
        Iterator iter = s.iterator();
        while (iter.hasNext()) {
            buffer.append(iter.next());
            if (iter.hasNext()) {
                buffer.append(delimiter);
            }
        }
        return buffer.toString();
    }
	
	public static List repeat(Object o, int num) {
		List l = new ArrayList(num);
		for (;num>0;num--) {
			l.add(o);
		}
		return l;
	}
	
	public static int getNullableHashCode(Object o) {
		return (o == null) ? 0 : o.hashCode();
	}
	
	public static String getSingular(String plural) {
		if (plural.endsWith("ren")) {
			return plural.substring(0, plural.length()-3);
		} else if (plural.endsWith("ies")) {
			return plural.substring(0, plural.length()-3) + "y";
		} else if (plural.endsWith("s")) {
			return plural.substring(0, plural.length()-1);
		}
		throw new RuntimeException("Can't determine singular for '" + plural + "'");
	}
	
}
